module Fighter {
	/**联机模式--by王石 */
	export class OlUI extends eui.Component {
		/**联机模式单例 */
		public static OlUI_solo: OlUI;
		/**匹配按钮 */
		public marryBtn: eui.Group;
		/**匹配窗口 */
		public marryStata: eui.Group;
		/**vs标签 */
		public vs: eui.Label;
		/**匹配结果 */
		public marryMsg: eui.Label;
		/**倒计时 */
		public count_down: egret.Timer;
		/**倒计时数字 */
		public count_down_number: number;
		/**取消匹配按钮 */
		public cancelBtn: eui.Button;
		/**是否关闭 */
		public is_close: boolean;
		/**是否匹配上人 */
		public is_match: boolean;
		/**虚假头像 */
		public fake_EnemyHead: egret.Bitmap;
		/**显示的容器 */
		private showPanel: any;

		public constructor() {
			super();
			this.skinName = "resource/assets/PlaneE/Ol_UI.exml";
			this.addEventListener(egret.Event.ADDED_TO_STAGE, this.add_to_stage, this);
			this.addEventListener(egret.Event.REMOVED_FROM_STAGE, this.removeFromStage, this);
			OlUI.OlUI_solo = this;
			this.is_close = false;
			this.is_match = false;
		}
		private add_to_stage() {
			this.marryBtn.visible = true;
			this.vs.visible = false;
			this.marryStata.visible = false;
			this.cancelBtn.visible = false;
			this.marryBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, webNetworkMananger.getInstance().user_ready, this);
			this.cancelBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.cancel_marry, this);


		}
		/**离开舞台 */
		private removeFromStage() {
			if (Main.wxHead && Main.wxHead.parent) {
				Main.wxHead.parent.removeChild(Main.wxHead);
			}
			if (Main.EnemyHead && Main.EnemyHead.parent) {
				Main.EnemyHead.parent.removeChild(Main.EnemyHead);
			}
		}
		/**准备结果 */
		public ready_result(tmpmessage: awesomepackage.s2cReady) {
			this.marryStata.visible = true;
			if (tmpmessage.errorCode == 0) {
				this.count_down_number = 0;
				this.count_down = new egret.Timer(1000, 0);
				this.count_down.addEventListener(egret.TimerEvent.TIMER, this.count_down_show, this);
				this.count_down.start();

			} else {
				this.marryMsg.text = "匹配失败...";
				this.cancelBtn.visible = true;
			}
		}
		/**匹配倒计时显示 */
		public count_down_show() {
			this.cancelBtn.visible = false;
			this.marryBtn.visible = false;
			this.count_down_number++;
			this.marryMsg.text = "匹配中...\n" + (30 - this.count_down_number).toString() + "s";
			if (this.count_down_number >= 25) {
				this.marryMsg.text = "匹配成功\n还有" + (30 - this.count_down_number).toString() + "s开始游戏";
				if (this.count_down_number >= 30) {
					this.count_down.removeEventListener(egret.TimerEvent.TIMER, this.count_down_show, this);
					this.count_down.stop();
					GameUtil.PlayerMessage.instance.onePerson = true;
					this.is_match = false;
					StartUI.StartUI_sole.is_close = true;
					this.is_close = true;
				}
				// //加载微信头像
				// if (Main.wxHead) {
				// 	Main.wxHead.x = this.stage.stageWidth / 2 - 2 * Main.wxHead.width;
				// 	Main.wxHead.y = this.stage.stageHeight / 2 - 2 * Main.wxHead.height;
				// 	this.addChild(Main.wxHead);
				// 	//加载 预设头像
				// 	this.fake_EnemyHead.texture = RES.getRes("touxiang_png");
				// 	this.fake_EnemyHead.x = this.showPanel.stage.stageWidth / 2 + this.fake_EnemyHead.width;
				// 	this.fake_EnemyHead.y = this.showPanel.stage.stageHeight / 2 - 2 * this.fake_EnemyHead.height;
				// 	this.fake_EnemyHead.stage.addChild(this.fake_EnemyHead);
				// }
			}
		}


		/**匹配结果 */
		public marry_result(tmpmessage: awesomepackage.s2cGameStart) {
			if (tmpmessage.errorCode == 0) {
				this.is_match = true;
				this.count_down_number = 0;
				this.count_down.removeEventListener(egret.TimerEvent.TIMER, this.count_down_show, this);
				this.count_down.stop();
				//成功匹配				
				this.count_down = new egret.Timer(1000, 0);
				this.count_down.addEventListener(egret.TimerEvent.TIMER, this.Start_count_down, this);
				this.count_down.start();
			}
		}
		/**开始游戏倒计时 */
		public Start_count_down() {
			this.cancelBtn.visible = false;
			this.count_down_number++;
			this.marryMsg.text = "匹配成功\n还有" + (5 - this.count_down_number).toString() + "s开始游戏";
			if (this.count_down_number >= 5) {
				this.count_down.removeEventListener(egret.TimerEvent.TIMER, this.Start_count_down, this);
				this.count_down.stop();
				StartUI.StartUI_sole.is_close = true;
				this.is_match=false;
				this.is_close = true;
			}
			//加载微信头像
			if (Main.wxHead) {
				Main.wxHead.x = this.stage.stageWidth / 2 - 2 * Main.wxHead.width;
				Main.wxHead.y = this.stage.stageHeight / 2 - 2 * Main.wxHead.height;
				this.addChild(Main.wxHead);
			}
			if (Main.EnemyHead) {
				Main.EnemyHead.x = this.stage.stageWidth / 2 + Main.EnemyHead.width;
				Main.EnemyHead.y = this.stage.stageHeight / 2 - 2 * Main.EnemyHead.height;
				this.addChild(Main.EnemyHead);
				this.vs.visible = true;
			}

		}
		/**取消匹配 */
		public cancel_marry() {
			this.cancelBtn.removeEventListener(egret.TouchEvent.TOUCH_TAP, this.cancel_marry, this);
			this.marryStata.visible = false;
			this.marryBtn.visible = true;
		}
	}
}
